﻿// <copyright file="DemoPlugIn.cs" company="Pelican Design &amp; Development">
//      Copyright (c) Pelican Design &amp; Development. All Rights Reserved.
// </copyright>
// <author id="5c2316d3-622a-4a8d-816d-5054a48f415f">Arseni Mourzenko</author>

namespace PelicanDD.Samples.CacheInSandbox
{
    using System;
    using System.Diagnostics;
    using System.IO;

    /// <summary>
    /// Represents the demo plug-in running in partial trust context.
    /// </summary>
    public class DemoPlugIn : IPlugIn
    {
        /// <summary>
        /// Gets or sets the cache.
        /// </summary>
        public ICacheWrapper Cache { get; set; }

        /// <summary>
        /// Gets the name of the plug-in.
        /// </summary>
        public string Name
        {
            get
            {
                return "Demo plug-in";
            }
        }

        /// <summary>
        /// Creates a file containing sample text, or fails if there are no enough permissions.
        /// </summary>
        /// <param name="fileName">The full path to the file to write.</param>
        public void WriteSampleFile(string fileName)
        {
            File.WriteAllText(fileName, "Hello");
        }

        /// <summary>
        /// Stores a value into cache, then loads the same value from cache and returns it.
        /// </summary>
        /// <param name="value">The value to store in cache.</param>
        /// <returns>The value from cache.</returns>
        public string UseCache(string value)
        {
            const string KeyInCache = "sampleEntity";

            this.Cache.Add(KeyInCache, value, TimeSpan.FromMinutes(1));
            var valueFromCache = this.Cache.Get(KeyInCache);

            Debug.Assert(value.Equals(valueFromCache), "The value from cache is not the same as the original.");
            return valueFromCache as string;
        }
    }
}
